package handler;

import java.io.File;
import java.io.IOException;

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;

import windows.AddAngajatFrame;
import windows.Ferestre;

import data.Angajat;
import data.AngajatiData;
import data.Scoala;

public class Handler {
	public void actionPerformed(String msg, AngajatiData angajati,
			Ferestre fereastra) {
		if (msg.equals("loadDatabaseCommand")) {
			JFileChooser fileChooser = new JFileChooser();
			FileNameExtensionFilter filter = new FileNameExtensionFilter(
					".dat", "All files");
			fileChooser.setFileFilter(filter);
			int status = fileChooser.showOpenDialog(fereastra.getMainFrame()
					.getFrame());
			if (status == JFileChooser.APPROVE_OPTION) {
				File fisier = fileChooser.getSelectedFile();
				removeAngajati(angajati);
				fereastra.getLoad().LoadAngajati(fisier);
				fereastra.getMainFrame().getComboBox_Scoala().removeAllItems();
				for (Scoala school : angajati.getListaScoli())
					fereastra.getMainFrame().getComboBox_Scoala()
							.addItem(school.getNumeScoala());
			}
		} else if (msg.equals("exportDatabaseCommand")) {
			JFileChooser fileChooser = new JFileChooser();

			int status = fileChooser.showSaveDialog(fereastra.getMainFrame()
					.getFrame());
			if (status == JFileChooser.APPROVE_OPTION) {
				File fisier = fileChooser.getSelectedFile();

				try {
					fereastra.getSave()
							.saveAngajati(angajati, fisier.getPath());
				} catch (IOException e) {

					e.printStackTrace();
				}
			}
		} else if (msg.equals("addSchoolFrameCommand")) {

			fereastra.getAddSchoolFrame().setFrame();
			fereastra.setAddSchoolFrame();

		} 
		
		else if (msg.equals("modifySchoolFrameCommand")) {
			for (Scoala school : angajati.getListaScoli())
				if (fereastra.getMainFrame().getComboBox_Scoala()
						.getSelectedItem().equals(school.getNumeScoala()))
					angajati.setSelectedSchool(school);
			fereastra.getModifySchoolFrame().setFrame(
					angajati.getSelectedSchool());
			fereastra.setModifySchoolFrame();

		}

		else if(msg.equals("addSchoolCommand"))
		{
			Scoala school=new Scoala();
			school.setNumeScoala(
					fereastra.getAddSchoolFrame().getTextFieldSchoolName()
							.getText());
			school.setSchoolTown(
					fereastra.getAddSchoolFrame().getTextFieldSchoolTown()
							.getText());
			school.setSchoolStreet(
					fereastra.getAddSchoolFrame().getTextFieldSchoolStreet()
							.getText());
			school.setSchoolNumber(
					Integer.parseInt(fereastra.getAddSchoolFrame()
							.getTextFieldSchoolNumber().getText()));
			school.setSchoolPrincipal(
					fereastra.getAddSchoolFrame()
							.getTextFieldSchoolDirector().getText());
			school.setSchoolAccountant(
					fereastra.getAddSchoolFrame()
							.getTextFieldSchoolContabil().getText());
			
			fereastra.getMainFrame().getComboBox_Scoala()
			.addItem(school.getNumeScoala());
			
			fereastra.getAddSchoolFrame().getTextFieldSchoolName().setText(null);
			fereastra.getAddSchoolFrame().getTextFieldSchoolTown().setText(null);
			fereastra.getAddSchoolFrame().getTextFieldSchoolStreet().setText(null);
			fereastra.getAddSchoolFrame().getTextFieldSchoolNumber().setText(null);
			fereastra.getAddSchoolFrame().getTextFieldSchoolDirector().setText(null);
			fereastra.getAddSchoolFrame().getTextFieldSchoolContabil().setText(null);
			angajati.getListaScoli().add(school);

		}
		
		else if(msg.equals("cancelSchoolCommand"))
		{
			fereastra.getAddSchoolFrame().dispose();
		}
		else if (msg.equals("cancelModifySchoolCommand")) {
			fereastra.getModifySchoolFrame().dispose();
		}
		
		else if (msg.equals("modifySchoolCommand")) {
			angajati.getSelectedSchool().setNumeScoala(
					fereastra.getModifySchoolFrame().getTextFieldSchoolName()
							.getText());
			angajati.getSelectedSchool().setSchoolTown(
					fereastra.getModifySchoolFrame().getTextFieldSchoolTown()
							.getText());
			angajati.getSelectedSchool().setSchoolStreet(
					fereastra.getModifySchoolFrame().getTextFieldSchoolStreet()
							.getText());
			angajati.getSelectedSchool().setSchoolNumber(
					Integer.parseInt(fereastra.getModifySchoolFrame()
							.getTextFieldSchoolNumber().getText()));
			angajati.getSelectedSchool().setSchoolPrincipal(
					fereastra.getModifySchoolFrame()
							.getTextFieldSchoolDirector().getText());
			angajati.getSelectedSchool().setSchoolAccountant(
					fereastra.getModifySchoolFrame()
							.getTextFieldSchoolContabil().getText());

			SetAngajatiData(angajati);

			fereastra.getModifySchoolFrame().dispose();
			try {
				fereastra.getSave().saveAngajati(angajati);
			} catch (IOException e) {
				System.out.println("asd");
				e.printStackTrace();
			}

		}

		else if (msg.equals("removeSchoolCommand")) {
			if (fereastra.getMainFrame().getComboBox_Scoala().getItemCount() == 0)
				fereastra.getMainFrame().showError(
						"Nu exista nicio scoala in baza de date!!!");
			else {
				for (int i = 0; i < angajati.getListaScoli().size(); i++) {
					if (angajati
							.getListaScoli()
							.get(i)
							.getNumeScoala()
							.equals(fereastra.getMainFrame()
									.getComboBox_Scoala().getSelectedItem()
									.toString())) {
						angajati.getListaScoli().remove(i);
						fereastra
								.getMainFrame()
								.getComboBox_Scoala()
								.removeItem(
										fereastra.getMainFrame()
												.getComboBox_Scoala()
												.getSelectedItem());
					}
				}
			}
			try {
				fereastra.getSave().saveAngajati(angajati);
			} catch (IOException e) {

				e.printStackTrace();
			}
		} else if (msg.equals("selectSchoolCommand")) {
			if (fereastra.getMainFrame().getComboBox_Scoala().getItemCount() != 0) {
				for (Scoala school : angajati.getListaScoli())
					if (fereastra.getMainFrame().getComboBox_Scoala()
							.getSelectedItem().equals(school.getNumeScoala()))
						angajati.setSelectedSchool(school);
				fereastra.getActionFrame().setFrame();
				fereastra.setActionFrame();
				fereastra.getMainFrame().hideFrame();
				fereastra.getActionFrame().getFrame()
						.setTitle(angajati.getSelectedSchool().getNumeScoala());
				fereastra.getActionFrame().showFrame();
			} else
				fereastra.getMainFrame().showError(
						"Nu ati selectat nicio scoala!!!");
			fereastra.getActionFrame().setOpenedFirstTime(false);
		}

		else if (msg.equals("addAngajatCommand")) {

			fereastra.getActionFrame().hideFrame();
			fereastra.getAddAngajatFrame().setFrame();
			fereastra.setAddAngajatFrame();
			fereastra.getAddAngajatFrame().showFrame();
			fereastra.getAddAngajatFrame().setOpenedFirstTime(false);

		}

		else if (msg.equals("removeAngajatcommand")) {
			fereastra.getActionFrame().hideFrame();
			fereastra.getRemoveAngajatFrame().setFrame();
			fereastra.setRemoveAngajatFrame();
			fereastra.getRemoveAngajatFrame().showFrame();
			fereastra.getRemoveAngajatFrame().setOpenedFirstTime(false);

		} else if (msg.equals("modifyAngajatiCommand")) {

			fereastra.getActionFrame().hideFrame();
			fereastra.getModifyFrame().setframe(angajati.getSelectedSchool());
			fereastra.setModifyFrame();
			fereastra.getModifyFrame().showFrame();
			fereastra.getModifyFrame().setOpenedFirstTime(false);
		} else if (msg.equals("reportGeneratorCommand")) {
			fereastra.getReportGeneratorFrame().setFrame();
			fereastra.getReportGeneratorFrame().showFrame();
			fereastra.setReportGeneratorFrame();
		}

		else if (msg.equals("selectEmployer")) {
			if (fereastra.getRemoveAngajatFrame()
					.getComboBox_RemoveNumeAngajat().getItemCount() > 1) {
				Angajat worker;
				int selectedIndex;
				selectedIndex = fereastra.getRemoveAngajatFrame()
						.getComboBox_RemoveNumeAngajat().getSelectedIndex();
				worker = angajati.getSelectedSchool().getListaAngajati()
						.get(selectedIndex);
				fereastra.getRemoveAngajatFrame()
						.getTextField_RemoveContAngajat()
						.setText(worker.getContAngajat());
				fereastra.getRemoveAngajatFrame()
						.getTextField_RemoveSumaAngajat()
						.setText(worker.getSuma().toString());
				fereastra.getRemoveAngajatFrame()
						.getTextField_RemoveBancaAngajat()
						.setText(worker.getBancaAngajat());
				if (angajati.getSelectedSchool().getListaAngajati().size() < 2) {
				} else if (selectedIndex == 0) {
					fereastra.getRemoveAngajatFrame().getPrevButton()
							.setEnabled(false);
					fereastra.getRemoveAngajatFrame().getNextButton()
							.setEnabled(true);
				} else if (selectedIndex == angajati.getSelectedSchool()
						.getListaAngajati().size() - 1) {
					fereastra.getRemoveAngajatFrame().getPrevButton()
							.setEnabled(true);
					fereastra.getRemoveAngajatFrame().getNextButton()
							.setEnabled(false);
				} else {
					fereastra.getRemoveAngajatFrame().getPrevButton()
							.setEnabled(true);
					fereastra.getRemoveAngajatFrame().getNextButton()
							.setEnabled(true);

				}
			}
		} else if (msg.equals("saveAngajat")) {
			AddAngajatFrame frame = fereastra.getAddAngajatFrame();
			Angajat newWorker = new Angajat();
			if (checkAngajatData(frame.getTextField_NumeAngajat().getText(),
					frame.getTextField_ContAngajat().getText(), frame
							.getTextField_SumaAngajat().getText())) {

				newWorker.setNumeScoala(angajati.getSelectedSchool()
						.getNumeScoala());
				newWorker.setSchoolTown(angajati.getSelectedSchool().getSchoolTown());
				newWorker.setSchoolStreet(angajati.getSelectedSchool().getSchoolStreet());
				newWorker.setSchoolNumber(angajati.getSelectedSchool().getSchoolNumber());
				newWorker.setSchoolPrincipal(angajati.getSelectedSchool().getSchoolPrincipal());
				newWorker.setSchoolAccountant(angajati.getSelectedSchool().getSchoolAccountant());
				
				newWorker.setNumeAngajat(frame.getTextField_NumeAngajat()
						.getText());
				newWorker.setContAngajat(frame.getTextField_ContAngajat()
						.getText());
				newWorker.setSuma(Integer.parseInt(frame
						.getTextField_SumaAngajat().getText()));
				newWorker.setBancaAngajat(frame.getComboBox_BancaAngajat()
						.getSelectedItem().toString());
				angajati.getSelectedSchool().getListaAngajati().add(newWorker);
				frame.getTextField_NumeAngajat().setText(null);
				frame.getTextField_ContAngajat().setText(null);
				frame.getTextField_SumaAngajat().setText(null);
				frame.getComboBox_BancaAngajat().setSelectedIndex(0);
				try {
					fereastra.getSave().saveAngajati(angajati);
				} catch (IOException e) {

					e.printStackTrace();
				}
			} else {
				fereastra.getAddAngajatFrame().getTextField_NumeAngajat()
						.setText(null);
				fereastra.getAddAngajatFrame().getTextField_ContAngajat()
						.setText(null);
				fereastra.getAddAngajatFrame().getTextField_SumaAngajat()
						.setText(null);
				fereastra.getAddAngajatFrame().getComboBox_BancaAngajat()
						.setSelectedIndex(0);
				fereastra.getAddAngajatFrame().showError(
						"Datele introduse nu sunt valide");
			}

		} else if (msg.equals("cancelAngajat")) {
			fereastra.getAddAngajatFrame().getTextField_NumeAngajat()
					.setText(null);
			fereastra.getAddAngajatFrame().getTextField_ContAngajat()
					.setText(null);
			fereastra.getAddAngajatFrame().getTextField_SumaAngajat()
					.setText(null);
			fereastra.getAddAngajatFrame().getComboBox_BancaAngajat()
					.setSelectedIndex(0);

		} else if (msg.equals("nextEmployer")) {
			Angajat worker;
			int selectedItem, numarAngajati;
			fereastra.getRemoveAngajatFrame().getPrevButton().setEnabled(true);
			selectedItem = fereastra.getRemoveAngajatFrame()
					.getComboBox_RemoveNumeAngajat().getSelectedIndex() + 1;
			worker = angajati.getSelectedSchool().getListaAngajati()
					.get(selectedItem);
			fereastra.getRemoveAngajatFrame().getComboBox_RemoveNumeAngajat()
					.setSelectedIndex(selectedItem);

			fereastra.getRemoveAngajatFrame().getTextField_RemoveContAngajat()
					.setText(worker.getContAngajat());
			fereastra.getRemoveAngajatFrame().getTextField_RemoveSumaAngajat()
					.setText(worker.getSuma().toString());
			fereastra.getRemoveAngajatFrame().getTextField_RemoveBancaAngajat()
					.setText(worker.getBancaAngajat());

			numarAngajati = angajati.getSelectedSchool().getListaAngajati()
					.size();
			if (selectedItem == numarAngajati - 1) {
				fereastra.getRemoveAngajatFrame().getNextButton()
						.setEnabled(false);
			}
		} else if (msg.equals("prevEmployer")) {
			Angajat worker;
			int selectedItem;
			fereastra.getRemoveAngajatFrame().getNextButton().setEnabled(true);
			selectedItem = fereastra.getRemoveAngajatFrame()
					.getComboBox_RemoveNumeAngajat().getSelectedIndex() - 1;
			worker = angajati.getSelectedSchool().getListaAngajati()
					.get(selectedItem);
			fereastra.getRemoveAngajatFrame().getComboBox_RemoveNumeAngajat()
					.setSelectedIndex(selectedItem);

			fereastra.getRemoveAngajatFrame().getTextField_RemoveContAngajat()
					.setText(worker.getContAngajat());
			fereastra.getRemoveAngajatFrame().getTextField_RemoveSumaAngajat()
					.setText(worker.getSuma().toString());
			fereastra.getRemoveAngajatFrame().getTextField_RemoveBancaAngajat()
					.setText(worker.getBancaAngajat());

			if (fereastra.getRemoveAngajatFrame()
					.getComboBox_RemoveNumeAngajat().getSelectedIndex() == 0) {
				fereastra.getRemoveAngajatFrame().getPrevButton()
						.setEnabled(false);
			}
		} else if (msg.equals("deleteEmployer")) {
			int selectedIndex;
			selectedIndex = fereastra.getRemoveAngajatFrame()
					.getComboBox_RemoveNumeAngajat().getSelectedIndex();
			if (angajati.getSelectedSchool().getListaAngajati().size() == 1) {
				angajati.getSelectedSchool().getListaAngajati().remove(0);
				fereastra
						.getRemoveAngajatFrame()
						.getComboBox_RemoveNumeAngajat()
						.removeItem(
								fereastra.getRemoveAngajatFrame()
										.getComboBox_RemoveNumeAngajat()
										.getSelectedItem());
				fereastra.getRemoveAngajatFrame()
						.getTextField_RemoveContAngajat()
						.setText("Cont angajat");
				fereastra.getRemoveAngajatFrame()
						.getTextField_RemoveSumaAngajat()
						.setText("Suma angajat");
				fereastra.getRemoveAngajatFrame()
						.getTextField_RemoveBancaAngajat()
						.setText("Banca angajat");
				fereastra.getRemoveAngajatFrame().getDeleteAngajatButton()
						.setEnabled(false);
			} else if (angajati.getSelectedSchool().getListaAngajati().size() - 1 == selectedIndex) {
				fereastra.getRemoveAngajatFrame()
						.getComboBox_RemoveNumeAngajat()
						.setSelectedIndex(selectedIndex - 1);
				fereastra.getRemoveAngajatFrame()
						.getComboBox_RemoveNumeAngajat()
						.removeItemAt(selectedIndex);
				angajati.getSelectedSchool().getListaAngajati()
						.remove(selectedIndex);
				fereastra.getRemoveAngajatFrame().getNextButton()
						.setEnabled(false);
			} else {
				fereastra.getRemoveAngajatFrame()
						.getComboBox_RemoveNumeAngajat()
						.setSelectedIndex(selectedIndex + 1);
				fereastra.getRemoveAngajatFrame()
						.getComboBox_RemoveNumeAngajat()
						.removeItemAt(selectedIndex);
				angajati.getSelectedSchool().getListaAngajati()
						.remove(selectedIndex);
				if (selectedIndex == 0)
					fereastra.getRemoveAngajatFrame().getPrevButton()
							.setEnabled(false);
			}
			try {
				fereastra.getSave().saveAngajati(angajati);
			} catch (IOException e) {

				e.printStackTrace();
			}

		} else if (msg.equals("saveModify")) {
			for (int i = 0; i < fereastra.getModifyFrame().getTabelAngajati()
					.getRowCount(); i++) {

				String dateAngajat = fereastra.getModifyFrame()
						.getTabelAngajati().getValueAt(i, 0).toString();
				angajati.getSelectedSchool().getListaAngajati().get(i)
						.setNumeAngajat(dateAngajat);
				dateAngajat = fereastra.getModifyFrame().getTabelAngajati()
						.getValueAt(i, 1).toString();
				angajati.getSelectedSchool().getListaAngajati().get(i)
						.setContAngajat(dateAngajat);
				dateAngajat = fereastra.getModifyFrame().getTabelAngajati()
						.getValueAt(i, 2).toString();
				angajati.getSelectedSchool().getListaAngajati().get(i)
						.setSuma(Integer.parseInt(dateAngajat));
				dateAngajat = fereastra.getModifyFrame().getTabelAngajati()
						.getValueAt(i, 3).toString();
				angajati.getSelectedSchool().getListaAngajati().get(i)
						.setBancaAngajat(dateAngajat);
				try {
					fereastra.getSave().saveAngajati(angajati);
				} catch (IOException e) {

					e.printStackTrace();
				}

			}
		} else if (msg.equals("reportGenerator")) {
			ExportFiles file = new ExportFiles(fereastra);
			if (fereastra.getReportGeneratorFrame().getCheckRaiffeisen()
					.getState())
				try {
					file.setRaifeissenExportFile(angajati);
					file.setRaifeissenPrintFile(angajati);
				} catch (Exception e) {

				}
			if (fereastra.getReportGeneratorFrame().getCheckTransilvania()
					.getState())
				try {
					file.setTransilvaniaExportFile(angajati);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			fereastra.getReportGeneratorFrame().getFrame().dispose();
		}

	}

	private void SetAngajatiData(AngajatiData angajati) {
		Scoala school = angajati.getSelectedSchool();
		for (Angajat angajat : school.getListaAngajati()) {
			angajat.setNumeScoala(school.getNumeScoala());
			angajat.setSchoolStreet(school.getSchoolStreet());
			angajat.setSchoolNumber(school.getSchoolNumber());
			angajat.setSchoolTown(school.getSchoolTown());
			angajat.setSchoolPrincipal(school.getSchoolPrincipal());
			angajat.setSchoolAccountant(school.getSchoolAccountant());
		}

	}

	private void removeAngajati(AngajatiData angajati) {

		if (angajati.getListaScoli().size() > 0) {
			angajati.getListaScoli().remove(0);
			removeAngajati(angajati);
		}
	}

	private boolean checkAngajatData(String nume, String cont, String suma) {
		if (nume.length() == 0)
			return false;
		if (cont.length() == 0)
			return false;
		if (suma.length() == 0)
			return false;
		try {
			Integer.parseInt(suma);
		} catch (NumberFormatException ex) {
			return false;
		}
		;
		return true;
	}
}